﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PIM_ATE
{
    public partial class FrmConfigAntenna : Form
    {
        public FrmConfigAntenna(int type)
        {
            InitializeComponent();
            this.Text = "测试配置";
            m_iType = type;
        }

        int m_iType;
        ClsDataConfigBase m_cfgBuilder;
        public const int TYPE_ANTENNA = 0;
        public const int TYPE_SPARAM = 1;
        public const int TYPE_RCU = 2;

        private void FrmConfigAntenna_Load(object sender, EventArgs e)
        {
            if (m_iType == TYPE_ANTENNA)
            {
                m_cfgBuilder = new ClsDataConfigLocalBuilder(GDefine.DBNamePIM);
            }
            else if (m_iType == TYPE_SPARAM)
            {
                m_cfgBuilder = new PIM_ATE.Electrical.ClsDataSParamBuilder(GDefine.DBNameELEC);
            }
            else if (m_iType == TYPE_RCU)
            {
                m_cfgBuilder = new ClsDataRcuConfigBuilder(GDefine.DBNameELEC);
            }
            else
            {
            }

            ctl_dvgCfg.ReadOnly = true;
            ctl_dvgCfg.RowHeadersVisible = false;
            ctl_dvgCfg.AllowUserToAddRows = false;
            ctl_dvgCfg.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            ctl_dvgCfg.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
            ctl_dvgCfg.MultiSelect = false;

            updateConfig();
        }

        private void ctl_btnCopy_Click(object sender, EventArgs e)
        {

        }

        private void ctl_btnDelCfg_Click(object sender, EventArgs e)
        {
            if (ctl_treeProcode.SelectedNode != null)
            {
                TreeNode tNode = ctl_treeProcode.SelectedNode;
                if (tNode.Name == "") return;

                if (MessageBox.Show("确认删除？（无法恢复！）", "警告", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    m_cfgBuilder.construct(tNode.Name);
                    if (m_cfgBuilder.delete() > 0)
                    {
                        updateConfig();
                    }
                    else
                    {
                        MessageBox.Show("删除失败");
                    }
                }
            }
        }

        private void ctl_btnNewCfg_Click(object sender, EventArgs e)
        {
            Form frmNewConfig = null;
            if (m_iType == TYPE_ANTENNA)
            {
                frmNewConfig = new FrmConfigNewAntenna(m_cfgBuilder as ClsDataConfigLocalBuilder);
            }
            else if (m_iType == TYPE_SPARAM)
            {
                frmNewConfig = new PIM_ATE.Electrical.FrmSConfigNewAntenna(m_cfgBuilder as PIM_ATE.Electrical.ClsDataSParamBuilder);
            }
            else if (m_iType == TYPE_RCU)
            {
                frmNewConfig = new FrmConfigRCUData(m_cfgBuilder as ClsDataRcuConfigBuilder);
            }
            else
            {
                return;
            }

            if (frmNewConfig.ShowDialog() == DialogResult.OK)
            {
                updateConfig();
            }
        }

        private void updateConfig()
        {
            ctl_dvgCfg.DataSource = null;
            ctl_treeProcode.Nodes.Clear();
            List<ProcodeNode> pNodesProcodes = m_cfgBuilder.getNodeList();
            TreeNode tNodesProcodes = new TreeNode();
            tNodesProcodes.Text = "配置-版本";
            foreach (var pNode in pNodesProcodes)
            {
                for (int i = 0; i < pNode.ListProcodeVersion.Count; i++)
                {
                    TreeNode n = new TreeNode();
                    n.Name = pNode.ListProcodeID[i];
                    n.Text = pNode.ListProcodeVersion[i];
                    n.Tag = pNode.Procode;

                    if (pNode.ListProcodeVersion[i] == "1.0")
                    {
                        n.Text = pNode.Procode;
                        tNodesProcodes.Nodes.Add(n);
                    }
                }
            }

            ctl_treeProcode.Nodes.Add(tNodesProcodes);
            ctl_treeProcode.Nodes[0].Expand();
        }

        private void ctl_treeProcode_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (ctl_treeProcode.SelectedNode != null)
            {
                TreeNode tNode = ctl_treeProcode.SelectedNode;
                if (tNode.Name == "") return;
                
                m_cfgBuilder.construct(tNode.Name);
                //List<ClsDataConfigLocal> lstConfig = m_cfgBuilder.download();
                //DataTable dtConfig = ClsDataConfigLocalBuilder.toTable(lstConfig);
                ctl_dvgCfg.DataSource = m_cfgBuilder.getProCodeTable();
                ctl_lblTips.Text = tNode.FullPath.ToString();
            }
        }

        private void ctl_btnReName_Click(object sender, EventArgs e)
        {

        }
    }
}
